<template>
  <div class="manage">
    <el-dialog title="提示" :visible.sync="dialogVisible" width="42%">
      <!-- 表单信息 -->
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        :inline="true"
        :rules="rules"
        :before-close="handleClose"
      >
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别" prop="birth">
            <el-option label="男" :value="1"></el-option>
            <el-option label="女" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期" prop="birth">
          <el-date-picker
            v-model="form.birth"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-DD"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="地址" prop="addr">
          <el-input v-model="form.addr" placeholder="请输入地址"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>

    <div class="manage-header">
      <el-button type="primary" @click="handleAdd">+ 新增</el-button>
      <!-- 搜索 -->
      <el-form :model="searchForm" :inline="true">
        <el-form-item>
          <el-input
            v-model="searchForm.name"
            placeholder="请输入名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table :data="tableData" style="width: 100%" height="80%" stripe="">
      <el-table-column prop="name" label="姓名"> </el-table-column>
      <el-table-column prop="sex " label="性别">
        <template slot-scope="scope">
          <span>{{ scope.row.sex == 1 ? "男" : "女" }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="age" label="年龄"> </el-table-column>
      <el-table-column prop="birth" label="出生日期"> </el-table-column>
      <el-table-column prop="addr" label="地址"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handlerEdit(scope.row)"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" @click="handlerDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pager">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        @current-change="handlePage"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getUser, addUser, editUser, delUser } from "../api";
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        name: "",
        age: "",
        sex: "",
        birth: "",
        addr: "",
      },
      rules: {
        name: [{ required: true, message: "请输入姓名" }],
        age: [{ required: true, message: "请输入年龄" }],
        sex: [{ required: true, message: "请选择性别" }],
        birth: [{ required: true, message: "请选择出生日期" }],
        addr: [{ required: true, message: "请输入地址" }],
      },
      tableData: [],
      modalType: 0, // 0表示新增的弹窗， 1表示编辑
      total: 0, //数据总条数
      pageData: {
        page: 1,
        limit: 10,
      },
      searchForm: {
        name: "",
      },
    };
  },
  methods: {
    // 提交用户校验
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 后续对表单数据的处理
          if (this.modalType == 0) {
            addUser(this.form).then(() => {
              // 获取table列表数据
              this.getList();
            });
          } else {
            editUser(this.form).then(() => {
              // 获取table列表数据
              this.getList();
            });
          }
          // 如果校验通过
          this.handleClose();
          // 关闭弹框
          this.dialogVisible = false;
        }
      });
    },
    handleClose() {
      // 防止数据回显,清空数据
      this.$refs.form.resetFields();
    },
    cancel() {
      this.handleClose();
      this.dialogVisible = false;
    },
    // 点击新增的回调
    handleAdd() {
      this.modalType = 0;
      this.dialogVisible = true;
    },
    // 编辑
    handlerEdit(row) {
      this.dialogVisible = true;
      this.modalType = 1;
      // 注意需要对当前的数据进行深拷贝, 而不是直接赋值
      this.form = JSON.parse(JSON.stringify(row));
    },
    // 删除
    handlerDelete(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delUser({ id: row.id }).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 获取table列表数据
    getList() {
      getUser({ params: { ...this.pageData, ...this.searchForm } }).then(
        ({ data }) => {
          this.tableData = data.list;
          this.total = data.count || 0;
        }
      );
    },
    // 切换页码的回调
    handlePage(val) {
      this.pageData.page = val;
      this.getList();
    },
    // 搜索列表的回调
    onSubmit() {
      this.getList();
    },
  },
  mounted() {
    this.getList();
  },
};
</script>

<style lang="less" scoped>
.manage {
  height: 90%;
  position: relative;
  .manage-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
.pager {
  margin-top: 20px;
  position: absolute;
  margin-left: 50%;
  transform: translateX(-50%);
}
</style>